package com.qk.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qk.common.PageResult;
import com.qk.dto.ClueDto;
import com.qk.dto.MarkFalseClueDto;
import com.qk.entity.Business;
import com.qk.entity.Clue;
import com.qk.entity.ClueTrackRecord;
import com.qk.mapper.BusinessMapper;
import com.qk.mapper.ClueMapper;
import com.qk.mapper.ClueTrackRecordMapper;
import com.qk.service.ClueService;
import com.qk.utils.CurrentUserHoler;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
public class ClueServiceImpl  extends ServiceImpl<ClueMapper, Clue> implements ClueService {
    @Autowired
    private ClueTrackRecordMapper clueTrackRecordMapper;
    @Autowired
    private ClueMapper clueMapper;
    //线索列表查询
    @Autowired
    private BusinessMapper businessMapper;
    @Override
    public PageResult<Clue> listClues(ClueDto clueDto) {
        Page<Clue> page = this.baseMapper.listClues(new Page<Clue>(clueDto.getPage(), clueDto.getPageSize()), clueDto);
        return new PageResult<Clue>(page.getTotal(), page.getRecords());
    }
//根据id查询线索详细信息
    @Override
    public Clue getClueById(Integer id) {
        return this.baseMapper.getClueById(id);

    }
//线索跟进
    @Transactional
    @Override
    public void trackClue(Clue clue) {
        //更新线索的状态为跟进中
        clue.setStatus(3);
        clue.setUpdateTime(LocalDateTime.now());
        this.updateById(clue);
        //添加线索的跟进记录
        ClueTrackRecord clueTrackRecord = new ClueTrackRecord();
        //为对象属性赋值
        clueTrackRecord.setClueId(clue.getId());
        //获取当前登录用户id
        clueTrackRecord.setUserId(CurrentUserHoler.getCurrentUser());
       // clueTrackRecord.setUserId(1);
        clueTrackRecord.setSubject(clue.getSubject());
        clueTrackRecord.setLevel(clue.getLevel());
        clueTrackRecord.setRecord(clue.getRecord());
        clueTrackRecord.setNextTime(clue.getNextTime());
        clueTrackRecord.setType(1);
        clueTrackRecord.setCreateTime(LocalDateTime.now());
        //添加数据
        clueTrackRecordMapper.insert(clueTrackRecord);
    }
//标记为伪线索
    @Transactional
    @Override
    public void handleClue(Integer id, MarkFalseClueDto markFalseClueDto) {
        Clue clue= new Clue();
        clue.setId(id);
        clue.setStatus(4);
        clueMapper.updateById(clue);
        //添加线索的跟进记录
        ClueTrackRecord clueTrackRecord= new ClueTrackRecord();
        clueTrackRecord.setClueId(id);
        clueTrackRecord.setUserId(CurrentUserHoler.getCurrentUser());
        clueTrackRecord.setRecord(markFalseClueDto.getRemark());
        clueTrackRecord.setType(0);
        clueTrackRecord.setFalseReason(markFalseClueDto.getReason());
        clueTrackRecord.setCreateTime(LocalDateTime.now());
        clueTrackRecordMapper.insert(clueTrackRecord);
    }
//转商机处理
    @Override
    public void toBusiness(Integer id) {
        Clue clue=clueMapper.selectById( id);
        //修改线索信息
        clue.setStatus(5);
        //修改时间
        clue.setUpdateTime(LocalDateTime.now());
        clueMapper.updateById(clue);
        //创建商机信息
        Business business= new Business();
        //拷贝线索信息
        BeanUtils.copyProperties(clue,business);
        //单独为其他字段赋值
        business.setId(null);
        business.setUserId(null);
        business.setNextTime(null);
        business.setStatus(1);
        business.setClueId(id);
        business.setCreateTime(LocalDateTime.now());
        business.setUpdateTime(LocalDateTime.now());
        //插入商机记录
        businessMapper.insert(business);
    }
}

